﻿Imports System.Math
Public Class centralTendency

    Public Function mean(ByVal input() As Double) As Double
        Return input.Average()
    End Function

    Public Function median(ByVal input() As Double) As Double
        Array.Sort(input)
        Dim length As Integer = input.GetLength(0)
        If length Mod 2 = 0 Then
            Return (input((length / 2) + 1) + input(length / 2)) / 2
        End If
        If length Mod 2 = 1 Then
            Return input((length + 1) / 2)
        End If
    End Function

    Public Function mode(ByVal input() As Double) As Double
        Array.Sort(input)
        Dim valueChangeIndice(0) As Integer
        For i As Integer = 0 To input.GetLength(0) - 2
            If (input(i) - input(i + 1)) < 0 Then
                valueChangeIndice(valueChangeIndice.GetLength(0) - 1) = i + 1
                ReDim Preserve valueChangeIndice(valueChangeIndice.GetLength(0))
            End If
        Next
        Dim maxoccurIndic(valueChangeIndice.GetLength(0) - 1) As Integer
        For i As Integer = 0 To valueChangeIndice.GetLength(0) - 1
            If i = 0 Then
                maxoccurIndic(i) = valueChangeIndice(i)
            Else
                maxoccurIndic(i) = valueChangeIndice(i) - valueChangeIndice(i - 1)
            End If
        Next
        Return input(valueChangeIndice(Array.IndexOf(maxoccurIndic, maxoccurIndic.Max) - 1))
    End Function

    Public Function GeometricMean(ByVal input() As Double) As Double
        Dim multiply As Double = 1
        For i As Integer = 0 To input.GetLength(0) - 1
            multiply = multiply * input(i)
        Next
        Return Math.Pow(multiply, (1 / input.GetLength(0)))
    End Function

    Public Function HarmonicMean(ByVal input() As Double) As Double
        Dim den As Double = 0
        For i As Integer = 0 To input.GetLength(0) - 1
            den = den + (1 / input(i))
        Next
        Return input.GetLength(0) / den
    End Function

    Public Function MidRange(ByVal input() As Double) As Double
        Return (input.Max + input.Min) / 2
    End Function


End Class

